package edu.uc.labs.MobilePrinting;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.log4j.Logger;

/**
 * A class that checks for and
 * installs needed files for the
 * MobilePrint Project
 *
 * @author Matt Silbernagel
 */
public class Installer {

    public boolean installed = false;
    
    /* The directory structure should look like this:
     * installDir/
     *      cache/
     *      icons/
     *          tray.png
     *      logs/
     *      ppd/         
     *      drivers/
     */
    public int size = 5;
    public String[] dirsNeeded = new String[size];
    public String trayIcon = null;
    public Configurator conf = new Configurator();
    public Logger logger = conf.getLogger();
    
    /**
     * Set up the directory structure
     */
    public Installer() {    	   
        dirsNeeded[0] = Configurator.CACHEDIR;		          // installDir
        dirsNeeded[1] = dirsNeeded[0] + Configurator.CACHE;   // cache
        dirsNeeded[2] = dirsNeeded[0] + Configurator.ICONS;   // icons
        dirsNeeded[3] = dirsNeeded[0] + Configurator.LOGS;    // logs
        dirsNeeded[4] = dirsNeeded[0] + Configurator.PPD;     // ppd for *NIX      
        trayIcon = dirsNeeded[2] + Configurator.DELIM + Configurator.TRAYICON;
    }

    public void install() {

        for (int i = 0; i < size; i++) {
            File f = new File(dirsNeeded[i]);
            if (!f.exists()) {
                logger.debug(f.getPath() + " does not exist. Attempting to create...");
                if (f.mkdir()) {
                    logger.debug("Successfully created the Directory: " + f.getPath());
                } else {
                    logger.debug("Was unable to create the Directory: " + f.getPath());
                }
            }
        }

        String imgName = Configurator.TRAYICON;
        File f = new File(dirsNeeded[2] + Configurator.DELIM + imgName);
        File f2 = new File(dirsNeeded[2] + Configurator.DELIM + Configurator.TRAYICON_PROGRESS);
        if (!f.exists()) {
            logger.debug("I need to put the image in the correct directory...");
            try {
                InputStream is = this.getClass().getResourceAsStream("images" + Configurator.DELIM + Configurator.TRAYICON);
                OutputStream os = new FileOutputStream(dirsNeeded[2] + Configurator.DELIM + Configurator.TRAYICON);
                byte[] buffer = new byte[10000];
                int length;
                while ((length = is.read(buffer)) > 0) {
                    os.write(buffer, 0, length);
                }
                os.close();
                is.close();
            } catch (NullPointerException e) {
                e.printStackTrace();
            } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


        }

        if (!f2.exists()) {
            logger.debug("I need to put the image in the correct directory...");
            try {
                InputStream is = this.getClass().getResourceAsStream("images" + Configurator.DELIM + Configurator.TRAYICON_PROGRESS);
                OutputStream os = new FileOutputStream(dirsNeeded[2] + Configurator.DELIM + Configurator.TRAYICON_PROGRESS);
                byte[] buffer = new byte[10000];
                int length;
                while ((length = is.read(buffer)) > 0) {
                    os.write(buffer, 0, length);
                }
                os.close();
                is.close();
            } catch (Exception e) {
                logger.debug(e.toString());
            }
        }      
    }

    /**
     * Getter for installed
     * @return true if all files are installed, false otherwise
     */
    public boolean isInstalled() {
        return installed;
    }

        
}
